package stark.app.function.beat;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ArrayUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.stark.beat.lib.core.BeatSettingManager;
import com.stark.beat.lib.core.BeatSoundType;
import com.stark.beat.lib.core.BeatSounder;
import com.stark.beat.lib.core.Beater;
import com.stark.beat.lib.core.BpmSpeedometer;

import stark.app.R;
import stark.app.databinding.FragmentBeaterBinding;
import stark.common.basic.base.BaseNoModelFragment;

/**
 * Author：daizhongbin on 2023/6/12
 * Email：1027154338@qq.com
 * Desc:
 */
public class BeaterFragment extends BaseNoModelFragment<FragmentBeaterBinding> {

    private BeatSettingManager mBeatSettingManager;
    private Beater mBeater;
    private BeatSounder mBeatSounder;
    private BpmSpeedometer mBpmSpeedometer;

    @Override
    protected int onCreate() {
        return R.layout.fragment_beater;
    }

    @Override
    protected void initView() {
        mBeatSettingManager = BeatSettingManager.getInstance();
        mBeater = Beater.getInstance();
        mBeatSounder = BeatSounder.getInstance();

        //人声喊拍
        updateHumanSoundText();
        mDataBinding.btnHumanSound.setOnClickListener(v -> {
            mBeatSettingManager.setSoundByHuman(!mBeatSettingManager.isSoundByHuman());
            updateHumanSoundText();
        });
        //闪光灯
        updateFlashText();
        mDataBinding.btnFlash.setOnClickListener(v -> {
            mBeatSettingManager.setOpenFlash(!mBeatSettingManager.isOpenFlash());
            updateFlashText();
        });
        //震动
        updateVibrateText();
        mDataBinding.btnVibrate.setOnClickListener(v -> {
            mBeatSettingManager.setOpenVibrate(!mBeatSettingManager.isOpenVibrate());
            updateVibrateText();
        });
        //增加、减少节拍
        updateBeatText();
        mDataBinding.btnAddBeat.setOnClickListener(v -> {
            mBeatSettingManager.setBeats(mBeatSettingManager.getBeats() + 1);
            updateBeatText();
        });
        mDataBinding.btnReduceBeat.setOnClickListener(v -> {
            mBeatSettingManager.setBeats(mBeatSettingManager.getBeats() - 1);
            updateBeatText();
        });
        //增加、减少音符
        updateNoteText();
        mDataBinding.btnAddNote.setOnClickListener(v -> {
            mBeatSettingManager.setNotes(mBeatSettingManager.getNotes() * 2);
            updateNoteText();
        });
        mDataBinding.btnReduceNote.setOnClickListener(v -> {
            mBeatSettingManager.setNotes(mBeatSettingManager.getNotes() / 2);
            updateNoteText();
        });
        //增加、减少bpm
        updateBpmText();
        mDataBinding.btnAddBpm.setOnClickListener(v -> {
            mBeatSettingManager.setBpm(mBeatSettingManager.getBpm() + 1);
            updateBpmText();
        });
        mDataBinding.btnReduceBpm.setOnClickListener(v -> {
            mBeatSettingManager.setBpm(mBeatSettingManager.getBpm() - 1);
            updateBpmText();
        });
        //设置音色
        mDataBinding.rvSounds.setLayoutManager(new GridLayoutManager(getContext(), 3));
        BeatSoundAdapter soundAdapter = new BeatSoundAdapter();
        soundAdapter.setSelectBeatSoundType(mBeatSettingManager.getBeatSoundType());
        soundAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                mBeatSettingManager.setBeatSoundType(soundAdapter.getItem(position));
                soundAdapter.setSelectBeatSoundType(soundAdapter.getItem(position));
            }
        });
        soundAdapter.setNewInstance(ArrayUtils.asList(BeatSoundType.values()));
        mDataBinding.rvSounds.setAdapter(soundAdapter);

        //开始、停止
        mDataBinding.btnStart.setOnClickListener(v -> {
            mBeater.start();
        });
        mDataBinding.btnStop.setOnClickListener(v -> {
            mBeater.stop();
        });

        mDataBinding.beatView.setBeats(mBeatSettingManager.getBeats());

        mDataBinding.btnCheckSpeed.setOnClickListener(v -> {
            if (mBpmSpeedometer == null) {
                mBpmSpeedometer = new BpmSpeedometer();
                mBpmSpeedometer.setListener(new BpmSpeedometer.IListener() {
                    @Override
                    public void onFirstSecondTap(int tap) {
                        if (tap == 1) {
                            mDataBinding.tvBpmSpeed.setText("-");
                        } else {
                            mDataBinding.tvBpmSpeed.setText("- -");
                        }
                    }

                    @Override
                    public void onBpmSpeed(int bpmValue) {
                        mDataBinding.tvBpmSpeed.setText("" + bpmValue);
                    }
                });
            }
            mBpmSpeedometer.tap();
        });
    }

    @Override
    protected void initData() {
        //给设置管理器设置
        mBeatSettingManager.addListener(mBeater);
        mBeatSettingManager.addListener(mBeatSounder);
        mBeatSettingManager.addListener(mDataBinding.beatView.getBeatSetListener());
        //给节拍器设置监听器
        mBeater.addListener(new Beater.IListener() {
            @Override
            public void onBeatCome(int beatIdx) {
                mDataBinding.beatView.setSelBeatIdx(beatIdx);
                if (mBeatSettingManager.isSoundByHuman()) {
                    mBeatSounder.playSound(beatIdx);
                } else {
                    mBeatSounder.playSound(mDataBinding.beatView.getSelBeatHeightIdx());
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBeater.stop();
        mBeatSettingManager.delListener(mBeater);
        mBeatSettingManager.delListener(mBeatSounder);
        mBeatSounder.release();
    }

    private void updateHumanSoundText() {
        mDataBinding.btnHumanSound.setText(mBeatSettingManager.isSoundByHuman() ? "人声喊拍开" : "人声喊拍关");
    }

    private void updateFlashText() {
        mDataBinding.btnFlash.setText(mBeatSettingManager.isOpenFlash() ? "闪光灯开" : "闪光灯关");
    }

    private void updateVibrateText() {
        mDataBinding.btnVibrate.setText(mBeatSettingManager.isOpenVibrate() ? "震动开" : "震动关");
    }

    private void updateBeatText() {
        mDataBinding.tvBeat.setText("当前节拍数为:" + mBeatSettingManager.getBeats());
    }

    private void updateNoteText() {
        mDataBinding.tvNotes.setText("当前音符数为:" + mBeatSettingManager.getNotes());
    }

    private void updateBpmText() {
        mDataBinding.tvBpm.setText("当前bpm为：" + mBeatSettingManager.getBpm());
    }
}
